package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import model.ConferencePaper;
import model.ProgramChair;
import model.Reviewer;
import model.SubProgramChair;

/**
 * This class is a view that is responsible for displaying
 * a list of reviewers to be assigned to review a conference paper.
 * @author Darrion Harris
 *
 */
@SuppressWarnings("serial")
public class SPCAvailableReviewersPanel extends Container{

	ProgramChair my_chair = SPCViewport.my_chair;
	SubProgramChair my_subchair = SPCViewport.my_subchair;
	String userName = SPCViewport.my_current_user_name;
	final Dimension row_item_width = new Dimension((SPCViewport.WIDTH - 50)/3, SPCViewport.ROW_HEIGHT);

	public SPCAvailableReviewersPanel(ConferencePaper the_paper) {
		this.setLayout(new BorderLayout());

		JPanel reviewerList = setUpReviewerList(the_paper);
		
		JPanel pad = new JPanel();
		pad.add(reviewerList);
		pad.setOpaque(false);
		
		this.add(pad, BorderLayout.CENTER);
		this.setBackground(Color.white);
	}
	
	/**
	 * 
	 * @param the_paper to assign a reviewer to
	 * @return the panel displaying a list of reviewers
	 */
	private JPanel setUpReviewerList(ConferencePaper the_paper) {
	      JPanel reviewerList = new JPanel();
	      reviewerList.setLayout(new BoxLayout(reviewerList, BoxLayout.PAGE_AXIS));
	      ArrayList<JLabel> list = new ArrayList<JLabel>();
	      list.add(new JLabel("Reviewer Name"));
	      list.add(new JLabel("Currently Reviewing"));
	      list.add(new JLabel("Assign"));
	      
	      JPanel header_row = new JPanel();
	      
	      header_row.setLayout(new FlowLayout());
	      header_row.setBackground(BaseViewFrame.BACKGROUND_COLOR);
	      for(JLabel j : list){
	    	  j.setPreferredSize(row_item_width);
	    	  header_row.add(j);
	      }
	      reviewerList.add(header_row);
	      reviewerList.add(new JSeparator());
	      
		  String author = the_paper.getAuthorName();
		  int count = 0;
		  for(Reviewer r : my_chair.getReviewers().values()) {
				for(ConferencePaper p : r.getManuscripts()){
					if(p.getTitle().equals(the_paper.getTitle())){
						count ++;
					}
				}
			}
		  
		  for (Reviewer reviewer : my_chair.getReviewers().values())
		  {
			  // if im the author for a conference paper, im not an available reviewer
			  // need to compare author name to reviewers username
			  // if the reviewer is the author
			  if(!(reviewer.getUser().equals(author))){				  
				  JPanel row = new JPanel();
			      row.setLayout(new FlowLayout());
			      
			      JLabel name = new JLabel(reviewer.getUser());
			      name.setPreferredSize(row_item_width);
				  
			      JLabel reviewCount = new JLabel(Integer.toString(reviewer.getManuscripts().size()));
				  reviewCount.setPreferredSize(row_item_width);
				  
				  Component assigned;
				  setBackground(BaseViewFrame.BACKGROUND_COLOR);
				  if(reviewer.getManuscripts().contains(the_paper)) {
					  assigned = new JLabel("Assigned");
					  assigned.setForeground(Color.GREEN);
					  assigned.setPreferredSize(row_item_width);
				  }
				  else if(reviewer.getManuscripts().size() < 4) {
					  if(count < 4){
						  assigned = createAssign2Button(the_paper, reviewer, count);
						  assigned.setPreferredSize(row_item_width);
					  }
					  else {
						  assigned = new JLabel("Paper at Capacity");
						  assigned.setEnabled(false);
						  assigned.setPreferredSize(row_item_width);
					  }
				  }
				  else {
					  assigned = new JLabel("Reviewer at Capacity");
					  assigned.setForeground(Color.RED);
					  assigned.setPreferredSize(row_item_width);
				  }
				  row.add(name);
				  row.add(reviewCount);
				  row.add(assigned);
				  row.setBorder(BorderFactory.createEtchedBorder());
				  row.setBackground(BaseViewFrame.BACKGROUND_COLOR);
				  reviewerList.add(row);
				  reviewerList.setBackground(BaseViewFrame.BACKGROUND_COLOR);
				  reviewerList.add(Box.createVerticalStrut(10));
			  }
		  }
		return reviewerList;
	}
	
	/**
	 * creates a button used to assign the reviewer to the paper.
	 * @param the_paper to assign the reviewer to
	 * @param the_reviewer the reviewer being assigned
	 * @param the_count the count of papers this reviewer has been assigned.
	 * @return the button used to assign the reviewer to the paper.
	 */
	private JButton createAssign2Button(final ConferencePaper the_paper, final Reviewer the_reviewer, final int the_count){
		JButton button = new JButton("Assign");
		button.addActionListener(new AbstractAction()
	      {  
			/**
			 * 
			 */
			private static final long serialVersionUID = 6891477318345184137L;

			@Override
	        public void actionPerformed(final ActionEvent the_event)
	        {
	        	the_reviewer.setManuscript(the_paper);
	        	SPCViewport.back();
	        }
	      });
		return button;
    }
}
